package com.imay.cocos;

import com.alibaba.fastjson.JSON;
import com.imay.cocos.model.CocosClickModel;
import com.imay.cocos.model.CocosDataModel;
import com.imay.cocos.model.CocosScrollModel;
import com.imay.cocos.model.CocosSizeModel;
import com.imay.cocos.model.CocosStatusModel;
import com.imay.cocos.model.CocosTypeModel;
import com.imay.live.helper.ThreadManager;
import com.imay.live.model.CocosOpenWebViewModel;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.proto.helper.ProtoSocketCallBack;
import com.imay.live.proto.helper.ProtoSocketFactory;
import com.imay.live.proto.user.UserInfoCard;

/**
 * Created by hexiao on 2017/4/20.
 */

public class IMayCocosNetManager {

    private static IMayCocosNetManager instance;
    private OnCocosRoomListener mRoomListener;
    private OnCocosShopListener mShopListener;
    private OnCocosRoomHouseListener mRoomHouseListener;
    private OnCocosUpdateListener mUpdateListener;
    private int launcherState = 1;//更新状态 1 检查 2 正在更新资源 3 完成 4 APK升级
    private OnCocosShortLiveListener mShortLiveListener;

    public synchronized static IMayCocosNetManager getInstance() {
        if (instance == null) {
            instance = new IMayCocosNetManager();
        }
        return instance;
    }

    public int getLauncherState() {
        return launcherState;
    }

    public synchronized void setLauncherState(int launcherState) {
        this.launcherState = launcherState;
    }

    //生成socket请求的json字符串
//    private static ProtoRequestParams generateSocketParams(int messageId, Object object) {
//        int seqId = generateSeqId();
//        String command = COMMAND_PREFIX_SOCKET + seqId;
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("msg_id", messageId);
//        jsonObject.put("seq", seqId);
//        jsonObject.put("timeout", DEFAULT_TIMEOUT);
//        jsonObject.put("content", JSON.toJSON(object));
//        return new ProtoRequestParams(REQUEST_TYPE_SOCKET, command, jsonObject.toJSONString());
//    }

    //请求登录
//    public static ProtoRequestParams requestLogin(int phoneCountry, long phone, String password) {
//        AccountLoginReq request = new AccountLoginReq();
//        request.phoneCountry = phoneCountry;
//        request.phone = phone;
//        request.secret = MD5Util.getMD5(phone + password);
//        request.device = getUserDeviceInfo();
//        return generateSocketParams(MessageId.accountLoginReq, request);
//    }

    public void cocos2SDK_event(String jsonData) {
        final CocosDataModel cocosModel = JSON.parseObject(jsonData, CocosDataModel.class);
        if (cocosModel == null) {
            return;
        }
        ThreadManager.post(new Runnable() {
            @Override
            public void run() {
                if (mRoomListener != null) {
                    if (CocosValue.ID_ON_SHOW_UI.equalsIgnoreCase(cocosModel.getId())) { //跳转UI
                        CocosTypeModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosTypeModel.class);
                        if (model != null) {
                            if (model.getmType() == CocosValue.TYPE_RECHARGE) {    //充值
                                mRoomListener.onCocosRechargeClick();
                            } else if (model.getmType() == CocosValue.TYPE_CHOOSE_USER) {   //送礼选人
                                mRoomListener.onCocosChooseUserClick();
                            } else if (model.getmType() == CocosValue.TYPE_BALANCE) {   //余额不足
                                mRoomListener.onCocosBalanceNotEnough();
                            } else if (model.getmType() == CocosValue.TYPE_USER_INFO_CARD) {   //显示用户资料卡
                                UserInfoCard userInfo = JSON.parseObject(JSON.toJSONString(model.getDt()), UserInfoCard.class);
                                mRoomListener.onCocosShowUserInfo(userInfo);
                            }
                        }
                    } else if (CocosValue.ID_ON_SHOW_BOTTOM_VIEW.equalsIgnoreCase(cocosModel.getId())) {   //显示隐藏底部view
                        CocosStatusModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosStatusModel.class);
                        mRoomListener.onCocosShowBottomView(model.getStatus() == CocosValue.STATUS_VIEW_SHOW);
                    } else if (CocosValue.ID_ON_SHOW_TOP_VIEW.equalsIgnoreCase(cocosModel.getId())) {  //显示隐藏顶部view
                        CocosStatusModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosStatusModel.class);
                        mRoomListener.onCocosShowTopView(model.getStatus() == CocosValue.STATUS_VIEW_SHOW);
                    } else if (CocosValue.ID_ON_SHOW_ACTION_VIEW.equalsIgnoreCase(cocosModel.getId())) {  //显示隐藏底部按钮
                        CocosStatusModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosStatusModel.class);
                        mRoomListener.onCocosShowActionView(model.getStatus() == CocosValue.STATUS_VIEW_SHOW);
                    } else if (CocosValue.ID_ON_SHOW_GAME_BUTTON.equalsIgnoreCase(cocosModel.getId())) {    //显示游戏隐藏按钮
                        CocosStatusModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosStatusModel.class);
                        mRoomListener.onCocosShowGameButton(model.getStatus() == CocosValue.STATUS_GAME_BUTTON_SHOW, model.getArrow() == CocosValue.STATUS_GAME_BUTTON_ARROW_UP);
                    } else if (CocosValue.ID_ON_CHANGE_COCOS_ORDER.equalsIgnoreCase(cocosModel.getId())) {  //切换cocos层级
                        CocosStatusModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosStatusModel.class);
                        mRoomListener.onCocosChangeHierarchy(model.getStatus() == CocosValue.STATUS_COCOS_ORDER_TOP);
                    } else if (CocosValue.ID_ON_SET_BOTTOM_MARGIN.equalsIgnoreCase(cocosModel.getId())) { //设置app底部下拉菜单高度
                        CocosSizeModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosSizeModel.class);
                        mRoomListener.onCocosSetBottomMargin(model.getHeight());
//                    } else if (CocosValue.ID_ON_ENTER_ROOM.equalsIgnoreCase(cocosModel.getId())) {  //跳转房间
//                        CocosTypeModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosTypeModel.class);
//                        CocosEnterRoomModel roomModel = JSON.parseObject(JSON.toJSONString(model.getDt()), CocosEnterRoomModel.class);
//                        mRoomListener.onCocosEnterRoom(roomModel.getRoom());
                    } else if (CocosValue.ID_ON_CLICK_BOUND.equalsIgnoreCase(cocosModel.getId())) {  //点击指定区域
                        CocosClickModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosClickModel.class);
                        mRoomListener.onCocosClickBound(model.getKey());
                    } else if (CocosValue.ID_ON_GET_ROOM_DATA.equalsIgnoreCase(cocosModel.getId())) {  //获取房间数据
                        mRoomListener.onCocosGetRoomData();
                    } else if (CocosValue.ID_ON_SET_SCROLL_ENABLED.equalsIgnoreCase(cocosModel.getId())) {
                        CocosTypeModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosTypeModel.class);
                        CocosScrollModel scrollModel = JSON.parseObject(JSON.toJSONString(model.getDt()), CocosScrollModel.class);
                        mRoomListener.onCocosSetHorizontalScrollEnabled(scrollModel.getSwitchH() == CocosValue.STATUS_SCROLL_ENABLED);
                        mRoomListener.onCocosSetVerticalScrollEnabled(scrollModel.getSwitchV() == CocosValue.STATUS_SCROLL_ENABLED);
                    }else if(CocosValue.ID_ON_SET_LIVE_CLOSE_EVENT.equalsIgnoreCase(cocosModel.getId())){
                        CocosStatusModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosStatusModel.class);
                        mRoomListener.onCocosSetLiveCloseEvent(model.getStatus()==1);
                    }else if(CocosValue.ID_ON_SHOW_H5_EVENT.equalsIgnoreCase(cocosModel.getId())){
                        CocosOpenWebViewModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosOpenWebViewModel.class);
                        mRoomListener.onCocosShowH5Event(model);
                    } else if (CocosValue.ID_ON_SHOW_GIFT_RED_POINT.equals(cocosModel.getId())) {
                        CocosStatusModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosStatusModel.class);
                        mRoomListener.onCocosShowGiftRedPoint(model.getStatus() == 1);
                    }else if (CocosValue.ID_ON_SHOW_OPEN_FIRST_CHARGE.equals(cocosModel.getId())){
                        mRoomListener.onCocosShowFirstRecharge();
                    }
                }
                if (mShopListener != null) {
                    if (CocosValue.ID_ON_CLOSE_SHOP_UI.equalsIgnoreCase(cocosModel.getId())) {
                        mShopListener.onCloseShopUI();
                    } else if (CocosValue.ID_ON_SHOW_UI.equalsIgnoreCase(cocosModel.getId())) {
                        CocosTypeModel model = JSON.parseObject(JSON.toJSONString(cocosModel.getData()), CocosTypeModel.class);
                        if (model != null) {
                            if (model.getmType() == CocosValue.TYPE_RECHARGE) {    //充值
                                mShopListener.onCocosRechargeClick();
                            }
                        }
                    }
                }
                if (mUpdateListener != null) {
                    if (CocosValue.ID_ON_LANUCHER_STATE.equalsIgnoreCase(cocosModel.getId())) { //启动时候热更新
                        mUpdateListener.onCocosShowAction(cocosModel.getData().toString());//{"state":1} 1  2 3 完成
                    } else if (CocosValue.ID_ON_LANUCHER_UPDATE.equalsIgnoreCase(cocosModel.getId())) { //
                        mUpdateListener.onAppUpdateAction(cocosModel.getData().toString());//
                    } else if (CocosValue.ID_ON_GET_IMAGE.equals(cocosModel.getId())) { // 图片获取
                        mUpdateListener.onCocosGetImgAction(cocosModel.getData().toString());
                    }
                }
                if (mShortLiveListener != null) {
                    if (CocosValue.ID_ON_SHORT_LIVE_OVER.equalsIgnoreCase(cocosModel.getId())) { //超胆播挑战完成动画结束
                        mShortLiveListener.onCocosEndShortLiveFinish();
                    }
                }
                if (mRoomHouseListener!=null){
                     if (CocosValue.ID_ON_OPENH5_EVENT.equalsIgnoreCase(cocosModel.getId())) { //大富婆建楼卡H5
                        mRoomHouseListener.onOpenH5(cocosModel.getData().toString());
                    }else if (CocosValue.ID_ON_CLOSE_UI_EVENT.equalsIgnoreCase(cocosModel.getId())) {
                        mRoomHouseListener.onClose();
                    }
                }
            }
        });
    }


    //网络数据发送转发
    public void cocos2SDK_network(String jsonData) {
        ProtoRequestHelper.task(ProtoSocketFactory.requestCocosData(jsonData)).setTag(this.hashCode()).requestFullData(new ProtoSocketCallBack() {
            @Override
            public void onRequestSuccess(String command, int msgId, String data) {
                IMayCocos.SDK2Cocos_network(msgId, data);
            }

            @Override
            public void onRequestError(String command, int code, String message) {

            }
        });
//
//        ProtoRequestHelper.task(ProtoSocketFactory.requestLogin(phoneCountry, phone, password)).setTag(this.hashCode()).request(new ProtoCallBack() {
//            @Override
//            public void onRequestSuccess(String stringCommand, String data) {
//
//            }
//
//            @Override
//            public void onRequestError(String stringCommand, int code, String message) {
//
//            }
//        });


//        IMayCocos.getInstance().SDK2Cocos_network(jsonString);

//        notify->
//        IMayCocos.getInstance().SDK2Cocos_network_Notify(JSONStringer);
    }
    public void addOnCocosRoomHouseListener(OnCocosRoomHouseListener listener) {
        this.mRoomHouseListener = listener;
    }
    public void removeOnCocosRoomHouseListener() {
            this.mRoomHouseListener = null;
    }
    public void addOnCocosRoomListener(OnCocosRoomListener listener) {
        this.mRoomListener = listener;
    }

    public void removeOnCocosRoomListener(OnCocosRoomListener listener) {
        if (this.mRoomListener == listener) {   //防止新的界面设置后再被旧的界面清除
            this.mRoomListener = null;
        }
    }

    public void setOnCocosShopListener(OnCocosShopListener listener) {
        this.mShopListener = listener;
    }

    public void setUpdateListener(OnCocosUpdateListener listener) {
        this.mUpdateListener = listener;
    }

    public void addOnCocosShortLiveListener(OnCocosShortLiveListener listener) {
        this.mShortLiveListener = listener;
    }

    public void removeOnCocosShortLiveListener(OnCocosShortLiveListener listener) {
        if (mShortLiveListener == listener) {
            this.mShortLiveListener = null;
        }
    }

    public interface OnCocosRoomListener {
        void onCocosShowBottomView(boolean show);

        void onCocosShowTopView(boolean show);

        void onCocosShowActionView(boolean show);

        void onCocosBalanceNotEnough();

        void onCocosRechargeClick();

        void onCocosChooseUserClick();

        void onCocosShowUserInfo(UserInfoCard userInfo);

        void onCocosSetBottomMargin(int margin);

        void onCocosChangeHierarchy(boolean top);

        void onCocosShowGameButton(boolean show, boolean up);

        //        void onCocosEnterRoom(RoomInfoModel room);
        void onCocosClickBound(int id);

        void onCocosGetRoomData();

        void onCocosSetHorizontalScrollEnabled(boolean enabled);

        void onCocosSetVerticalScrollEnabled(boolean enabled);

        void onCocosSetLiveCloseEvent(boolean allowClose);

        void onCocosShowH5Event(CocosOpenWebViewModel model);

        void onCocosShowGiftRedPoint(boolean visible);

        void onCocosShowFirstRecharge();
    }

    public interface OnCocosShopListener {
        void onCloseShopUI();

        void onCocosRechargeClick();

    }
    public interface OnCocosRoomHouseListener {
        void onOpenH5(String json);
        void onClose();
    }
    public interface OnCocosUpdateListener {
        void onCocosShowAction(String jsonData);

        void onAppUpdateAction(String jsonData);

        void onCocosGetImgAction(String jsonData);
    }

    public interface OnCocosShortLiveListener {
        void onCocosEndShortLiveFinish();   //超胆播挑战完成动画结束
    }
}
